package com.one.rope.mvp.web.common.redis;

import com.one.rope.mvp.basecore.util.JsonUtils;
import com.one.rope.mvp.web.common.dto.common.AreaInfoVO;

import com.one.rope.mvp.web.config.GlobalConfig;
import java.util.List;

/**
 * @author Weijian.Liu
 * @desc 全局缓存数据Redis操作
 * @date 2020/2/23 17:39
 */
public class CacheDataRedisOperator {

  /**
   * 保存行政区域列表
   */
  public static String saveAreaList(String key, List<AreaInfoVO> areaInfoList) throws Exception{
    String cacheDataKey = CommonRedisKeyGenerater.getCacheDataKey(key);
    return RedisClientUtil.setObject(cacheDataKey, areaInfoList);
  }

  /**
   * 获取行政区域列表
   */
  public static List<AreaInfoVO> getAreaList(String key) throws Exception{
    String cacheDataKey = CommonRedisKeyGenerater.getCacheDataKey(key);
    String jsonString = RedisClientUtil.get(cacheDataKey);
    return JsonUtils.stringToList(jsonString, AreaInfoVO.class);
  }

  /**
   * 删除行政区域列表
   */
  public static Long deleteAreaList(String key) throws Exception{
    String cacheDataKey = CommonRedisKeyGenerater.getCacheDataKey(key);
    return RedisClientUtil.del(cacheDataKey);
  }

  /**
   * 获取缓存更新标识
   * @return
   */
  public static String getCacheUpdateFlag() throws Exception{
    String key = CommonRedisKeyGenerater.getCacheUpdateFlagKey();
    return RedisClientUtil.get(key);
  }

  /**
   * 设置缓存更新标识
   * @return
   */
  public static String setCacheUpdateFlag(String value) throws Exception{
    String key = CommonRedisKeyGenerater.getCacheUpdateFlagKey();
    return RedisClientUtil.set(key, value);
  }


  /**
   * 保存公众号access_token
   * @param value
   * @return
   * @throws Exception
   */
  public static String saveOfficialAccessToken(String value) throws Exception{
    String key = CommonRedisKeyGenerater.getOfficialAccessTokenKey();
    return RedisClientUtil.setex(key, GlobalConfig.OFFICIAL_TOKEN__EXPIRES_SECOND, value);
  }

  /**
   * 获取公众号access_token
   * @return
   * @throws Exception
   */
  public static String getOfficialAccessToken() throws Exception{
    String key = CommonRedisKeyGenerater.getOfficialAccessTokenKey();
    return RedisClientUtil.get(key);
  }
}
